package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.constant.OrderType;
import pers.cj.pdos.entity.Packet;
import pers.cj.pdos.entity.PacketGoods;
import pers.cj.pdos.mapper.OrderRuleMapper;
import pers.cj.pdos.mapper.PacketGoodsMapper;
import pers.cj.pdos.mapper.PacketMapper;
import pers.cj.pdos.service.IPacketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.PacketSaleVo;
import pers.cj.pdos.vo.PacketVo;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class  PacketServiceImpl extends ServiceImpl<PacketMapper, Packet> implements IPacketService {
    @Autowired
    PacketMapper packetMapper;
    @Autowired
    PacketGoodsMapper packetGoodsMapper;
    @Autowired
    OrderRuleMapper orderRuleMapper;

    @Override
    public List<PacketSaleVo> listSaleByPacketId(Integer page, Integer limit, Boolean close, String search, String packetId, String storeId, String userId, Date date1, Date date2) {
        return packetMapper.listSaleByPacketId(Utils.genPage(page,limit),limit,close,search,packetId,storeId,userId,date1,date2);
    }

    @Override
    public Long countSaleByPacketId(Boolean close, String search, String packetId, String storeId, String userId, Date date1, Date date2) {
        return packetMapper.countSaleByPacketId(close, search, packetId, storeId, userId, date1, date2);
    }

    @Override
    public boolean checkName(String name,String companyId) {
        return packetMapper.selectCount(new QueryWrapper<Packet>().eq("name",name).eq("company_id",companyId)) > 0;
    }

    @Override
    public boolean checkNo(String no,String companyId) {
        return packetMapper.selectCount(new QueryWrapper<Packet>().eq("no",no).eq("company_id",companyId)) > 0;
    }

    @Override
    public void insert(Packet packet, List<PacketGoods> packetGoodsList) {
        packetMapper.insert(packet);
        if (!Utils.isEmpty(packetGoodsList)) {
            packetGoodsMapper.insertBatch(packetGoodsList);
        }
//        orderRuleMapper.addOrderRuleCurrent(packet.getCompanyId(), OrderType.TC.getType());
    }

    @Override
    public void update(Packet packet, List<PacketGoods> packetGoodsList) {
        packetMapper.updateById(packet);

        if (!Utils.isEmpty(packetGoodsList)) {
            packetGoodsMapper.deleteByPacket(packet.getId());
            packetGoodsMapper.insertBatch(packetGoodsList);
        }
    }

    @Override
    public List<PacketVo> listVoWithStock(String companyId, Integer page, Integer limit, Integer status, String content, String houseId) {
        return packetMapper.listVoWithStock(companyId,Utils.genPage(page,limit),limit,status,content,houseId);
    }

    @Override
    public List<PacketVo> listVoPage(String companyId, Integer page, Integer limit, Integer status, String content) {
        return packetMapper.listVo(companyId,Utils.genPage(page,limit),limit,status,content);
    }

    @Override
    public long count(String companyId, Integer status, String content) {
        return packetMapper.count(companyId,status,Utils.genLikeData(content));
    }

    @Override
    public PacketVo getVoById(String id) {
        return packetMapper.getVoById(id);
    }

    @Override
    public void delete(Packet packet) {
        packetGoodsMapper.deleteByPacket(packet.getId());
        packetMapper.deleteById(packet.getId());
    }

    @Override
    public List<PacketVo> listVoByIds(List<String> ids) {
        return packetMapper.listVoByIds(ids);
    }

    @Override
    public List<PacketVo> listVoByPacketListWithStock(String houseId, List list) {
        return packetMapper.listVoByPacketListWithStock(houseId,list);
    }

    @Override
    public boolean check(String id) {
        return Utils.check(packetMapper.check(id));
    }

}
